package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortReport;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortRankMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.server.manager.beautyescort.CrossGroupWeekUnionRankListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_RED_DOT_SYNC, desc = "红点")
public class CrossBeautyEscortRedDotCmd extends CrossCommand {

    List<Long> memberList;
    String unionUid = "";
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortRedDotReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortRedDotReqMsg.parseFrom(packet.getBytes());
        memberList = reqMsg.getMemberList();
        if(reqMsg.hasUnionUid()){
            unionUid = reqMsg.getUnionUid();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossEscortPlayerWrap escortPlayerWrap = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(escortPlayerWrap != null){
            BeautyEscortProto.CrossBeautyEscortRedDotRespMsg.Builder redDotRespMsg = BeautyEscortProto.CrossBeautyEscortRedDotRespMsg.newBuilder();
            redDotRespMsg.addAllBeauty(escortPlayerWrap.getEscortingBeautyIdList());
            if(escortPlayerWrap.getIdleEscortCartData() == null){
                redDotRespMsg.setIdleCart(false);
            }else {
                redDotRespMsg.setIdleCart(true);
            }
            int lastReadTime = escortPlayerWrap.getUserData().getLastReadReportTime();
            for (Map.Entry<Long, EscortReport> reportEntry : escortPlayerWrap.getEscortReportList()) {
                if(reportEntry.getValue().getLatestRobTime() > escortPlayerWrap.getUserData().getLastReadReportTime()){
                    redDotRespMsg.setReport(1);
                    lastReadTime = reportEntry.getValue().getLatestRobTime();
                }
            }
            for(long uid : memberList){
                CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.getEscortPlayer(serverId, uid);
                if(playerWrap != null){
                    int i = 1;
                    for (Map.Entry<Long, EscortReport> reportEntry : playerWrap.getEscortReportList()) {
                        if(i > CrossBeautyEscortMgr.MY_REPORT_NUM){
                            break;
                        }
                        if(reportEntry.getValue().getLatestRobTime() > lastReadTime){
                            redDotRespMsg.setUnionReport(1);
                            lastReadTime = reportEntry.getValue().getLatestRobTime();
                        }
                        i++;
                    }
                }
            }
            redDotRespMsg.setMyRank(-1);
            if(!StringUtils.isNullOrEmpty(unionUid)){
                int preWeekId = CrossBeautyEscortRankMgr.getPreWeekId();
                int groupId = CrossBeautyEscortRankMgr.getWeekServerGroup(preWeekId, serverId);
                CrossGroupWeekUnionRankListModel rankListModel = CrossBeautyEscortRankMgr.getCrossGroupWeekUnionRankListModel(preWeekId, groupId);
                if(rankListModel != null){
                    redDotRespMsg.setMyRank(rankListModel.getMyRank(unionUid));
                }
            }
            escortPlayerWrap.getUserData().setLastReadReportTime(lastReadTime);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_RED_DOT_SYNC_FROM_CROSS, redDotRespMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
